package leetCode;

public class t707 {
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addAtHead(7);
        myLinkedList.addAtHead(2);
        myLinkedList.addAtHead(1);
        myLinkedList.addAtIndex(3,0);
        myLinkedList.deleteAtIndex(2);
        myLinkedList.addAtHead(6);
        myLinkedList.addAtTail(4);

        System.out.println(myLinkedList.get(4));
    }
}

//["MyLinkedList","addAtHead","addAtHead","addAtHead","addAtIndex","deleteAtIndex","addAtHead","addAtTail","get","addAtHead","addAtIndex","addAtHead"]
//        [[],[7],[2],[1],[3,0],[2],[6],[4],[4],[4],[5,0],[6]]
class MyLinkedList {
    class ListNode{
        int val;
        ListNode next;
        ListNode(int val,ListNode next){
            this.val=val;
            this.next=next;
        }
    }
    static ListNode dummyHead;
    public MyLinkedList() {
        dummyHead=new ListNode(0,null);
    }

    public int get(int index) {
        ListNode head=dummyHead;
        int i=0;
        while (head.next!=null){
            if (i==index){
                return head.next.val;
            }
            head=head.next;
            i++;
        }
        return -1;
    }

    public void addAtHead(int val) {
        dummyHead.next=new ListNode(val,dummyHead.next);
    }

    public void addAtTail(int val) {
        ListNode head=dummyHead;
        ListNode newNode=new ListNode(val,null);
        while (head.next!=null){
            head=head.next;
        }
        head.next=newNode;

    }

    public void addAtIndex(int index, int val) {
        ListNode head=dummyHead;
        int i=0;
        boolean flag=true;
        while (head.next!=null||i==index){
            if (i==index||index<0){
                head.next=new ListNode(val,head.next);
                return;
            }
            head=head.next;
            i++;
        }
    }

    public void deleteAtIndex(int index) {
        ListNode head=dummyHead;
        int i=0;
        while (head.next!=null){
            if (i==index){
                head.next=head.next.next;
                return;
            }
            head=head.next;
            i++;
        }
    }
}